﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _2011DBProject
{
    public partial class PaymentProcessing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("onclick", "return confirm('확인을 누르시면 결제가 완료됩니다. \\n중복결제가 될 수 있으니 주의하세요.');");

            if ((string)(Session["USER"]) == null)
            {
                Response.Redirect("default.aspx?isNoSession=1");
            }
        
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView dr = e.Item.DataItem as DataRowView;
                Button btnPay = e.Item.FindControl("btnPay") as Button;
                btnPay.CommandArgument = dr["PRODUCT_NO"].ToString();
            }
        }
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //DataSet1 ds = new DataSet1();

            if (e.CommandName == "selectPay")
            {
                Label2.Visible = true;
                table44.Visible = true;
                //Label4.Text = e.CommandArgument.ToString();
                DataSet1 ds = new DataSet1();
                DataSet1TableAdapters.MERCHANDISETableAdapter taMer = new DataSet1TableAdapters.MERCHANDISETableAdapter();
                taMer.Connection.ConnectionString = Settings.strConnectionString;

                taMer.fillDataProduct(ds.MERCHANDISE, Convert.ToDecimal(e.CommandArgument));

                Label4.Text = ds.MERCHANDISE[0].PRODUCT_NAME;
                Label5.Text = ds.MERCHANDISE[0].SERVICE_PROVIDED;
                lblHid.Text = (e.CommandArgument).ToString();
            }
        }

        

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet1 ds = new DataSet1();
            DataSet1TableAdapters.PAYMENTTableAdapter taPay = new DataSet1TableAdapters.PAYMENTTableAdapter();
            taPay.Connection.ConnectionString = Settings.strConnectionString;

            taPay.ScalarQueryMaxPaymentNo((string)(Session["USER"]));

            DataSet1.PAYMENTRow r = ds.PAYMENT.NewPAYMENTRow();
            decimal maxPayNo = Convert.ToDecimal(taPay.ScalarQueryMaxPaymentNo((string)(Session["USER"])));

            r.ACCOUNT_ID = (string)(Session["USER"]);
            r.PAYMENT_NO = maxPayNo + 1;
            r.PRODUCT_NO = Convert.ToDecimal(lblHid.Text);
            
            string maxEndDate = "2000";
            DateTime maxEndDateType = DateTime.Now.AddDays(-10);

            if(!(taPay.ScalarQueryMaxDate((string)(Session["USER"]), maxPayNo) == null)){
                maxEndDateType = (DateTime)taPay.ScalarQueryMaxDate((string)(Session["USER"]), maxPayNo);
                maxEndDate = (taPay.ScalarQueryMaxDate((string)(Session["USER"]), maxPayNo)).ToString();
            }

            string nowDate = (DateTime.Now).ToString();

            int addDay;
            //int tmpV = Convert.ToInt32(lblHid.Text);
            switch (Convert.ToInt32(lblHid.Text))
            {
                case 1:
                    addDay = 7;
                    break;
                case 2:
                    addDay = 30;
                    break;
                case 3:
                    addDay = 90;
                    break;
                case 4:
                    addDay = 30;
                    break;
                default:
                    addDay = 0;
                    break;
            }

            if (maxEndDate.CompareTo(nowDate) > 0)
            {
                r.PAYMENT_DATE = DateTime.Now;
                r.END_DATE = maxEndDateType.AddDays(addDay);
            }
            else
            {
                r.PAYMENT_DATE = DateTime.Now;
                r.END_DATE = DateTime.Now.AddDays(addDay);
            }

            ds.PAYMENT.AddPAYMENTRow(r);
            taPay.Update(ds.PAYMENT);

            DataSet1TableAdapters.ACCOUNTTableAdapter taACC = new DataSet1TableAdapters.ACCOUNTTableAdapter();
            taACC.Connection.ConnectionString = Settings.strConnectionString;
            DataSet1.ACCOUNTDataTable accdata = taACC.GetDataByACCOUNT_ID((string)Session["USER"]);
            
            DataSet1.ACCOUNTRow ar = accdata[0];

            if(Convert.ToDecimal(lblHid.Text) == 1 || Convert.ToDecimal(lblHid.Text) == 2 || Convert.ToDecimal(lblHid.Text) == 4)
            {
                ar.GRADE_NO = 2;
            }
            else{
                ar.GRADE_NO = 3;                                //등급 바꾸고
            }

            taACC.Update(accdata);
            //updateStats();

            lblHid.Text = "결제가 정상적으로 완료되었습니다.";
            lblHid.Visible = true;

            try
            {
                DataSet1TableAdapters.VIRTUAL_LOCKERROOMTableAdapter taVir = new DataSet1TableAdapters.VIRTUAL_LOCKERROOMTableAdapter();
                taVir.Connection.ConnectionString = Settings.strConnectionString;
                DataSet1.VIRTUAL_LOCKERROOMRow vr = ds.VIRTUAL_LOCKERROOM.NewVIRTUAL_LOCKERROOMRow();

                vr.ACCOUNT_ID = (string)Session["USER"];            //기본골프채 넣어주고
                vr.ITEM_NO = 10001;
                vr.QTY = 1;
                ds.VIRTUAL_LOCKERROOM.AddVIRTUAL_LOCKERROOMRow(vr);
                //taVir.Update(ds.VIRTUAL_LOCKERROOM);
                vr = ds.VIRTUAL_LOCKERROOM.NewVIRTUAL_LOCKERROOMRow();
                vr.ACCOUNT_ID = (string)Session["USER"];            //골프공 넣어주고
                vr.ITEM_NO = 10093;
                vr.QTY = 24;
                ds.VIRTUAL_LOCKERROOM.AddVIRTUAL_LOCKERROOMRow(vr);
                taVir.Update(ds.VIRTUAL_LOCKERROOM);
            }
            catch
            {
                lblHid.Text = "결제가 정상적으로 완료되었습니다.";
                lblHid.Visible = true;
            }
        }

        protected void btnGoLogout_onclick(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        protected void btnGoServer_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Server.aspx");
        }
    }
}
